package org.erepublik.company.scanner;

import java.util.Arrays;
import java.util.HashMap;

import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.erepublik.company.comparators.ProductComparatorByPriceInGold;
import org.erepublik.company.product.EProductAttribute;
import org.erepublik.company.product.Product;
import org.erepublik.company.product.ProductDesign;
import org.erepublik.company.product.ProductKey;
import org.erepublik.company.product.Ticket;
import org.erepublik.company.service.ICompanyCountryService;
import org.erepublik.company.service.IVatService;
import org.erepublik.company.service.JSONExchangeRateService;

/**
 * Ticket scanner company
 *
 * @author Kacper Gorski 'Railman' railman85@gmail.com
 */
public class TicketScanner extends ProductScanner<Ticket> {

    @Override
    protected Ticket[] getEmptyTable() {
        return new Ticket[0];
    }
    @Override
    protected Ticket getNewProduct() {
        return new Ticket();
    }

    public Ticket[] getProductsFromCountry(ECountry country, IVatService vatService, ICompanyCountryService companyService,
            JSONExchangeRateService currency) throws Exception {
  
        ProductDesign productDesign = new ProductDesign(EProductType.TICKET);
        HashMap<ProductKey, Product> map = new HashMap<ProductKey, Product>();

        for (int i = 20; i <= 100; i += 20) {
            productDesign.setAttributeValue(EProductAttribute.MOVING_DISTANCE, i);
            Product[] scannedProducts = getProductList(country, productDesign, vatService, companyService, currency);
            for (Product product : scannedProducts) {
                ProductKey key = new ProductKey(product.getCompanyOwner().getId(), country);
                if (!map.containsKey(product)) {
                    map.put(key, product);
                }
            }
            if (scannedProducts.length<10)
                break;
        }
        Ticket[] products = map.values().toArray(new Ticket[0]);
        sortProductByPrice(products);
        return products;
    }


    
    
    private void sortProductByPrice(Product[] products) {
        Arrays.sort(products, new ProductComparatorByPriceInGold());
    }




    
    
    
}
